package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class Deptcontroller {
    @Autowired
   private   DeptService deptService;
    @Autowired
    private EmpService empService;
    @GetMapping
    public Result findAll(){
        List<Dept> list= deptService.findAll();
        return Result.success(list);
    }
    @PostMapping
    public Result save(@RequestBody Dept dept){
        deptService.save(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
   public Result deleteById(@PathVariable("id") Integer id){
       if (empService.selectByDeptId(id)<=0){
        deptService.deleteById(id);
        return Result.success();
       }else {
           return Result.error("该部门下还有员工 不能进行该操作");
       }
   }


    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Dept dept= deptService.findById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return  Result.success( );
    }
}
